<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
<script>
var Query={
	fields:{},
	set:function(s1,id){
		var ulid=id||'sh';
		var fields=this.fields[ulid];
		
		var li=$(s1).parent();
		var s3=li.find(".s3");
		s3.val("");
		var type=fields[$(s1).val()].vtype;
		s3.unbind('focus');
		s3.focus(function(e){
			if(type=='t'){
				new WdatePicker(this);
			}
		});
	},
	add:function(id){
		var ulid=id||'sh';
		var nli=$('#'+ulid+' li:first').clone();
		nli.appendTo($('#'+ulid));
		nli.append("<a href='#' onclick='Query.del(this)'>删除</a>");
	},
	del:function (t){
		$(t).parent().remove();
	},
	param:function(id){
		var ulid=id||'sh';
		var q=this.fields[ulid];
		var sql=[];
		$('#'+ulid+' li').each(function(){
			var s1=$(this).find(".s1").val();
			var s2=$(this).find(".s2").val();
			var s3=$(this).find(".s3").val();
			var str="";
			if(s3==''){
				str+=s1+" is null";
			}else{
				switch(q[s1].vtype){
					case 's':
						str+=s1+" "+s2+" "+"'"+s3+"'";
						break;
					case 'd':
						if($.trim(s3)){
							str+=s1+" is null";
						}else{
							str+=s1+" "+s2+" "+s3;
						}
						break;
					case 't':
						str+=s1+" "+s2+"to_char('"+s3+"','yyyy-MM-dd')";
						break;				
				}
			}
			sql.push(str);
		});
		return sql.join(" and ");
	}
} 
$.fn.query=function(fields,id){
	var ulid=id||'sh';
	Query.fields[ulid]=fields;
	$(this).append("<ul id='"+ulid+"'><li></li></ul>");
	var s1=$("<select class='s1' onchange='Query.set(this,\""+ulid+"\")'></select>");
	for(var f in fields){
		s1.append("<option value='"+f+"'>"+fields[f].text+"</option>");
	}
	
	var ops={'=':'等于','<':'小于'};
	var s2=$("<select class='s2'></select>");
	for(var op  in ops){
		s2.append("<option value='"+op+"'>"+ops[op]+"</option>");
	}
	
	var s3=$('<input type="text" class="s3"/>');
	
	s1.appendTo($('#'+ulid+' li:first'));
	s2.appendTo($('#'+ulid+' li:first'));
	s3.appendTo($('#'+ulid+' li:first'));
	Query.set($('#'+ulid+' li:first .s1'),ulid);
	
	$('#queryAdd').click(function(){Query.add(ulid)});
}





$(function(){
	//字符串s,数字d,时间t
	var fields={
		name:{text:'姓名',vtype:'s'},
		age:{text:'年龄',vtype:'d'},     
		birth:{text:'生日',vtype:'t'}
	};
	$('#query').query(fields);
});
</script>
</head>
<body>
<div id="query">
	<input type="button" value="新增" id="queryAdd"/>
	<input type="button" value="参数" onclick="alert(Query.param());"/>
</div>
</body>
</html>
